package com.bpmn.flow.controller;

import com.bpmn.common.annotation.ResponseInfoWrapper;
import com.bpmn.common.pojo.PageResult;
import com.bpmn.flow.controller.query.DefinitionQuery;
import com.bpmn.flow.controller.vo.ProcessDefinitionVo;
import com.bpmn.flow.service.DefinitionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wangzongrun
 */
@Tag(name = "流程定义相关")
@RestController
@RequiredArgsConstructor
@ResponseInfoWrapper
@RequestMapping(value = "definition")
public class DefinitionController {

    private final DefinitionService definitionService;

    /**
     * 流程定义查询接口
     */
    @Operation(summary = "流程定义查询接口")
    @PostMapping(value = "/list")
    public PageResult<ProcessDefinitionVo> getFlowDefinitions(DefinitionQuery definitionQuery) {
        return definitionService.getFlowDefinitions(definitionQuery);
    }

    /**
     * 激活或者挂起流程定义
     *
     * @param processDefinitionId 流程定义id
     */
    @Operation(summary = "激活或者挂起流程定义")
    @PostMapping("/state/{processDefinitionId}")
    public Boolean updateDefinitionState(@PathVariable String processDefinitionId) {
        return definitionService.updateDefinitionState(processDefinitionId);
    }

    /**
     * 查看流程定义xml文件
     *
     * @param processDefinitionId 流程定义id
     */
    @Operation(summary = "查看流程定义xml文件")
    @GetMapping("/xml/{processDefinitionId}")
    public Map<String, Object> definitionXml(@PathVariable String processDefinitionId) {
        Map<String, Object> map = new HashMap<>();
        String xmlStr = definitionService.definitionXml(processDefinitionId);
        map.put("xml", Arrays.asList(xmlStr.split("\n")));
        map.put("xmlStr", xmlStr);
        return map;
    }


}
